<template>
  <div>
    <ul class="list">
      <li v-for="(item, index) in list" :key="index" :class="active === index ? 'active' : ''" @click="liClick(index)">
        <div class="avatar">
          <Avatar :src="item.profilePhotoPath" size="large"/>
          <div class="sex">
            <Icon type="female" size="14" color="#FFB6C1" v-if="item.sex === 2"></Icon>
            <Icon type="male" size="14" color="#2b85e4" v-if="item.sex === 1"></Icon>
          </div>
        </div>
        <div class="info">
          <p>{{item.nickname}}</p>
        </div>
        <div class="is_new" v-if="item.count && item.count !== 0 && active !== index">{{item.count}}</div>
      </li>
    </ul>
    <!--<p @click="historyMessage" style="text-align: center;line-height: 30px;cursor: pointer;" v-show="showMore">查看三天历史</p>-->
  </div>
</template>

<script>
export default {
  name: 'online-list',
  data () {
    return {
      active: 0,
      list: [],
      openidArr: []
      // showMore: true
    }
  },
  methods: {
    clear () {
      this.list = []
      this.openidArr = []
      this.active = 0
    },
    setOnlineList (data) {
      if (!this.list[0]) {
        this.list = data
        this.list.forEach(item => {
          this.openidArr.push(item.openid)
        })
        return true
      }
      return false
    },
    addOnlineList (data) {
      if (this.openidArr.indexOf(data.senderOpenid) === -1) {
        this.$ajax.post('https://www.gztang.cn/admin/manager/user/info/by/openid', data.senderOpenid).then(res => {
          res.data.data.openid = data.senderOpenid
          this.$emit('addOnline', res.data.data)
          res.data.data.count = 1
          this.list.push(res.data.data)
          this.openidArr.push(data.senderOpenid)
        })
      }
    },
    newMessage (data) {
      let i = this.openidArr.indexOf(data.senderOpenid)
      if (i === this.active) {
        return
      }
      if (i !== -1) {
        let count = 1
        if (this.list[i].count) {
          count = ++this.list[i].count
          delete this.list[i].count
        }
        this.$set(this.list, i, {...this.list[i], count})
      }
    },
    liClick (index) {
      if (this.active === index) {
        return
      }
      this.list[index].count = 0
      this.active = index
      this.$emit('changeTab', this.list[index].openid)
    },
    addHistory (data) {
      this.clear()
      this.setOnlineList(data)
    }
  },
  created () {
  }
}
</script>

<style scoped>
  .list li{
    padding: 10px;
    box-sizing: border-box;
    overflow: hidden;
    position: relative;
  }
  .list li:hover{
    background-color: #e9eaec;
  }
  .avatar{
    float: left;
    position: relative;
  }
  .sex{
    position: absolute;
    right: 0;
    bottom: 0;
  }
  .info{
    float: left;
    margin-left: 10px;
  }
  .list li.active{
    border-left: 2px solid #2d8cf0;
    background-color: #dddee1;
  }
  .is_new{
    position: absolute;
    bottom: 8px;
    right: 8px;
    width: 14px;
    height: 14px;
    border-radius: 100%;
    background: #FF0000;
    text-align: center;
    line-height: 14px;
    color: #fff;
  }
</style>
